<template>
  <h1>员工列表练习</h1>
  <input type="text" placeholder="请输入员工姓名" v-model="emp.name">
  <input type="text" placeholder="请输入员工工资" v-model="emp.salary">
  <input type="text" placeholder="请输入员工岗位" v-model="emp.job">
  <button @click="add">点我添加</button>
  <hr>
  <table border="1px">
    <tr>
      <th>员工编号</th>
      <th>员工姓名</th>
      <th>员工工资</th>
      <th>员工岗位</th>
    </tr>
    <tr v-for="(item,index) in arr">
      <td>{{index+1}}</td>
      <td>{{item.name}}</td>
      <td>{{item.salary}}</td>
      <td>{{item.job}}</td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";

//1.定义一个员工对象用来保存收集到的数据
const emp = ref({name:'',salary:'',job:''});
//3.定义一个员工数组用来保存多个员工对象
const arr = ref([]);
//2.定义添加员工的方法
const add = ()=>{
  //添加之前需要对数据进行非空校验
  if(emp.value.name.trim()=='' || emp.value.salary.trim()=='' || emp.value.job.trim()==''){
    alert('请输入完整的信息！');
    return;
  }
  //4.每收集好一个员工数据，就将员工对象添加到数组中
  arr.value.push(emp.value);
  //5.每次存入数组后，清空输入框的数据
  emp.value = {name:'',salary:'',job:''};
  //emp.value = {};
}
</script>

<style scoped>

</style>